package com.sport.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	System.out.println("loginInterceptor..............afterCompletion");
		
	}
	
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("loginInterceptor ............postHandle");
	}
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object arg2) throws Exception {
		StringBuffer url=req.getRequestURL();
		//如果是登录页面则放行
		if(req.getRequestURI().indexOf("LoginAction")>=0){
			System.out.println("this is login");
			return true;
		}
		HttpSession session = req.getSession();
		//如果用户已登录也放行
		if(session.getAttribute("user")!=null){
			return true;
		}
		if(session.getAttribute("manager")!=null){
			return true;
		}
		req.getRequestDispatcher("login.jsp").forward(req, resp);
		System.out.println("loginInterceptor..............preHandle");
	
		
		return false;
	}
	
}
